package com.roads.engineering.controller;

import java.util.List;

import com.roads.common.constant.UserConstants;
import com.roads.common.utils.StringUtils;
import com.roads.project.domain.ProInstitutionDetail;
import com.roads.project.service.IProInstitutionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.roads.common.annotation.Log;
import com.roads.common.core.controller.BaseController;
import com.roads.common.core.domain.AjaxResult;
import com.roads.common.enums.BusinessType;
import com.roads.engineering.domain.EngPlanScale;
import com.roads.engineering.service.IEngPlanScaleService;
import com.roads.common.utils.poi.ExcelUtils;
import com.roads.common.core.page.TableDataInfo;

/**
 * 工程计划比例Controller
 * 
 * @author nhp
 * @date 2023-03-17
 */
@RestController
@RequestMapping("/engineering/plan")
public class EngPlanScaleController extends BaseController {

    @Autowired
    private IEngPlanScaleService service;

    @Autowired
    private IProInstitutionService institutionService;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('engineering:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(EngPlanScale model) {
        startPage();
        List<EngPlanScale> list = service.selectModelList(model);
        return getDataTable(list);
    }

    /**
     * 导出
     */
    @PreAuthorize("@ss.hasPermi('engineering:plan:export')")
    @Log(title = "工程计划比例", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(EngPlanScale model) {
        List<EngPlanScale> list = service.selectModelList(model);
        ExcelUtils<EngPlanScale> util = new ExcelUtils<EngPlanScale>(EngPlanScale.class);
        return util.exportExcel(list, "工程计划比例数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('engineering:plan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(service.getModelById(id));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('engineering:plan:add')")
    @Log(title = "工程计划比例", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EngPlanScale model) {
        // 检查同一个机构下 日期是否存在
        if (UserConstants.NOT_UNIQUE.equals(service.checkDateUnique(model))) {
            return AjaxResult.error("新增计划比例'" + model.getDate() + "'失败，所属年月已存在");
        }
        model.setCreateBy(getUsername());
        return toAjax(service.insert(model));
    }

    /**
     * 生成
     */
    @PreAuthorize("@ss.hasPermi('engineering:plan:gen')")
    @Log(title = "工程计划比例", businessType = BusinessType.GEN)
    @PostMapping("/gen")
    public AjaxResult gen(String institutionCode) {
        ProInstitutionDetail detail = institutionService.getDetailModelByInnerCode(institutionCode);
        String result = validateInstitution(detail);
        if (StringUtils.isNotEmpty(result)) {
            return error(result);
        }
        return toAjax(service.gen(detail, getUsername()));
    }

    /**
     * 验证标段是否存在，标段开始日期，截止日期是否为空
     *
     * @param detail 标段编码
     * @return 结果
     */
    private String validateInstitution(ProInstitutionDetail detail) {
        if (null == detail) {
            return "生成标段工期失败，未找到相对应的标段";
        }
        if (null == detail.getBeginDate()) {
            return "生成标段工期失败，标段开始时间为空";
        }
        if (null == detail.getEndDate()) {
            return "生成标段工期失败，标段截止时间为空";
        }
        return "";
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('engineering:plan:edit')")
    @Log(title = "工程计划比例", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EngPlanScale model) {
        return toAjax(service.update(model, getUsername()));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('engineering:plan:remove')")
    @Log(title = "工程计划比例", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(service.deleteByIds(ids));
    }
}
